package com.hooper.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hooper.hosp.model.hosp.HospitalSet;
import com.hooper.hosp.service.HospitalSetService;
import com.hooper.hosp.vo.HospitalSetVo;
import com.hooper.hosp.vo.order.SignInfoVo;
import com.hooper.hospital.common.exception.HospitalException;
import com.hooper.hospital.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import static com.hooper.hospital.common.result.ResultCodeEnum.DATA_ERROR;
import static com.hooper.hospital.common.result.ResultCodeEnum.PARAM_EMPTY;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/09/24/13:56
 */
@Api(value = "Hospital setting Api")
@RestController
@RequestMapping("/admin/hosp/setting")
//@CrossOrigin
public class HospSetController {


    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private HospitalSetService hospSetService;

    @ApiOperation(value = "Get all hosp settings (list)")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> test() {
        return Result.ok(hospSetService.list(null));
    }

    @ApiOperation(value = "delete one setting by its id")
    @DeleteMapping("{id}")
    public Result<Boolean> delById(@PathVariable Long id) {
        if (id == null) {
            return Result.fail();
        }
        return Result.ok(hospSetService.removeById(id));
    }

    @ApiOperation(value = "conditional query with pages")
    @PostMapping("/page/{currentPage}/{pageSize}")
    @SuppressWarnings("unchecked")
    public Result<IPage<HospitalSet>> queryPages(@PathVariable Integer currentPage,
                                                 @PathVariable Integer pageSize,
                                                 @RequestBody(required = false) HospitalSetVo conditions) {

        Page<HospitalSet> page = new Page<>(currentPage, pageSize);
        //if there are no conditions, return all page data
        if (conditions == null) {
            return Result.ok(hospSetService.page(page, null));
        }

        LambdaQueryWrapper<HospitalSet> lqw = new LambdaQueryWrapper<>();
        //check if the attributes are empty
        // dynamically add the conditions
        lqw.like(StringUtils.isNotEmpty(conditions.getHosname()), HospitalSet::getHosname, conditions.getHosname())
                .eq(StringUtils.isNotEmpty(conditions.getHoscode()), HospitalSet::getHoscode, conditions.getHoscode())
                .orderByDesc(HospitalSet::getUpdateTime);
        hospSetService.page(page, lqw);
        return Result.ok(page);
    }

    @ApiOperation("add a hosp setting")
    @PostMapping("/add")
    public Result<Boolean> addOneSetting(@RequestBody HospitalSet hospitalSet) {
        if (hospitalSet == null) {
            throw new HospitalException(PARAM_EMPTY);
        }
        boolean saved = hospSetService.saveOne(hospitalSet);
        if (!saved) {
            throw new HospitalException(DATA_ERROR);
        }
        return Result.ok();
    }

    @ApiOperation("get hosp setting by its id")
    @ApiParam(name = "id", value = "the hosp id")
    @GetMapping("getSetting/{id}")
    public Result<HospitalSet> getById(@PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            throw new HospitalException(PARAM_EMPTY);
        }
        HospitalSet entity = hospSetService.getById(id);

        return entity == null ? Result.fail() : Result.ok(entity);
    }

    @ApiOperation("update the data by hosp id")
    @PostMapping("update/{id}")
    public Result<Boolean> updateById(@PathVariable String id, @RequestBody HospitalSet newSet) {
        if (StringUtils.isEmpty(id) || newSet == null) {
            throw new HospitalException(PARAM_EMPTY);
        }
        //get the previous setting by its id
        HospitalSet preHospSet = hospSetService.getById(id);

        //if the object is null, throw the exception
        if (preHospSet == null) {
            throw new HospitalException(DATA_ERROR);
        }

        //give the id to the new setting
        newSet.setId(preHospSet.getId());

        //update
        return hospSetService.updateOneById(newSet) ? Result.ok() : Result.fail();

    }

    @ApiOperation("delete by ids")
    @DeleteMapping("deleteBatch")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            throw new HospitalException(PARAM_EMPTY);
        }
        return hospSetService.removeByIds(ids) ? Result.ok() : Result.fail();
    }

    @ApiOperation("change hosp status")
    @PostMapping("status/{id}/{status}")
    public Result<Boolean> changeStatus(@PathVariable Long id, @PathVariable Integer status) {
        return hospSetService.lockOrUnlock(id, status) ? Result.ok() : Result.fail();
    }

    @ApiOperation("send signature key by id")
    @PostMapping("sendKey/{id}")
    public Result<Boolean> sendSignatureKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //todo send message
        return Result.ok();
    }
    
    @ApiOperation(value = "获取医院签名信息")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable("hoscode") String hoscode) {
        return hospSetService.getSignInfoVo(hoscode);
    }

}
